package com.ibeeking.gmc.global.g.rest.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaJsapiService;
import com.ibeeking.found.common.entity.open.*;
import com.ibeeking.found.common.enums.OpenApiConfigEnum;
import com.ibeeking.found.common.enums.WxCpAppTypeEnum;
import com.ibeeking.found.common.enums.WxOpenAppTypeEnum;
import com.ibeeking.gmc.global.api.common.dto.JsapiSignatureDTO;
import com.ibeeking.gmc.global.api.common.query.JsapiSignatureQuery;
import com.ibeeking.gmc.global.g.rest.config.prop.RedisProperties;
import com.ibeeking.gmc.global.g.rest.service.IOpenAuthService;
import com.ibeeking.gmc.global.g.rest.service.IOpenConfigService;
import com.ibeeking.gmc.global.g.rest.service.IWxOpenService;
import com.ibeeking.gmc.global.service.common.constants.RedisKeyConstant;
import com.ibeeking.gmc.global.service.common.vo.OpenConfigVO;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.redis.utils.RedisUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpAgentJsapiSignature;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpTpRedissonConfigImpl;
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceImpl;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
import me.chanjar.weixin.open.api.WxOpenMaService;
import me.chanjar.weixin.open.api.WxOpenMpService;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 禁止在此类加其他业务方法
 *
 * @ClassName WxMpService
 * @Description
 * @Author ibeeking
 * @Date 2020-12-30 10:29
 **/
@Service
@EnableConfigurationProperties({RedisProperties.class})
public class WxOpenServiceImpl implements IWxOpenService {

    @Resource
    private IOpenConfigService openConfigServiceImpl;

    @Resource
    private IOpenAuthService openAuthServiceImpl;

    @Autowired
    private RedisProperties redisProperties;

    @Resource
    private RedisUtils redisUtils;

    /**
     * 获取Jsapi签名串
     * @param
     * @return
     */
    @Override
    public JsapiSignatureDTO createJsapiSignature(JsapiSignatureQuery query){
        if(query == null) return null;
        Long tId = null;
        String tenantId = query.getTenantId();
        String appId = query.getAppId();
        String url = query.getUrl();
        if(StringUtils.isEmpty(appId))  throw new BusinessException("appId为空！");
        if(StringUtils.isEmpty(url))  throw new BusinessException("签名url为空！");
        OpenAuthQuery authQuery = new OpenAuthQuery();
        if(StringUtils.isNotEmpty(tenantId))
            tId = Long.valueOf(tenantId);
        authQuery.setTenantId(tId);
        authQuery.setAppId(appId);
        OpenAuthDTO openAuthDTO = openAuthServiceImpl.queryOpenAuthOne(authQuery);
        if(openAuthDTO ==null ) throw new BusinessException("读取租户的开放平台参数openAuthDTO出错！");
        JsapiSignatureDTO result = new JsapiSignatureDTO();
        result.setTenantId(tenantId);
        result.setAppId(appId);
        result.setUrl(url);
        try {
            if(OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_OPEN.getCode() == openAuthDTO.getConfigType()){
                if(WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_MA.getCode() == openAuthDTO.getConfigSubType()){
                    WxOpenMaService wxOpenMaService = getBean().getWxOpenMaService(tId, appId);
                    WxJsapiSignature wxJsapiSignature =  wxOpenMaService.getJsapiService().createJsapiSignature(url);
                    if(wxJsapiSignature!=null) {
                        result.setSignature(wxJsapiSignature.getSignature());
                        result.setNonceStr(wxJsapiSignature.getNonceStr());
                        result.setTimestamp(wxJsapiSignature.getTimestamp());
                    }
                }else if(WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_MP.getCode() == openAuthDTO.getConfigSubType()){
                    WxOpenMpService wxOpenMpService = getBean().getWxOpenMpService(tId, appId);
                    WxJsapiSignature wxJsapiSignature = wxOpenMpService.createJsapiSignature(url);
                    if(wxJsapiSignature!=null) {
                        result.setSignature(wxJsapiSignature.getSignature());
                        result.setNonceStr(wxJsapiSignature.getNonceStr());
                        result.setTimestamp(wxJsapiSignature.getTimestamp());
                    }
                }
            } else if(OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode() == openAuthDTO.getConfigType()){
                if(WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode() == openAuthDTO.getConfigSubType()){
                    WxCpService wxCpService = getBean().getWxCpService(tId, appId);
                    if(StringUtils.isEmpty(query.getCorpId())) {
                        WxJsapiSignature wxJsapiSignature = wxCpService.createJsapiSignature(url);
                        if(wxJsapiSignature!=null) {
                            result.setSignature(wxJsapiSignature.getSignature());
                            result.setNonceStr(wxJsapiSignature.getNonceStr());
                            result.setTimestamp(wxJsapiSignature.getTimestamp());
                        }
                    }else{
                        WxCpAgentJsapiSignature wxJsapiSignature = wxCpService.createAgentJsapiSignature(url);
                        if(wxJsapiSignature!=null) {
                            result.setAgentid(wxJsapiSignature.getAgentid());
                            result.setCorpId(wxJsapiSignature.getCorpid());
                            result.setSignature(wxJsapiSignature.getSignature());
                            result.setNonceStr(wxJsapiSignature.getNonceStr());
                            result.setTimestamp(wxJsapiSignature.getTimestamp());
                        }
                    }
                }else if(WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_TP.getCode() == openAuthDTO.getConfigSubType()){
                    WxCpTpService wxCpTpService = getBean().getWxCpTpService(tId, appId);
                    if(StringUtils.isEmpty(query.getCorpId())) {
                        WxJsapiSignature wxJsapiSignature = wxCpTpService.createSuiteJsApiTicketSignature(url, query.getCorpId());
                        if(wxJsapiSignature!=null) {
                            result.setSignature(wxJsapiSignature.getSignature());
                            result.setNonceStr(wxJsapiSignature.getNonceStr());
                            result.setTimestamp(wxJsapiSignature.getTimestamp());
                        }
                    }else{
                        WxJsapiSignature wxJsapiSignature = wxCpTpService.createAuthCorpJsApiTicketSignature(url, query.getCorpId());
                        if(wxJsapiSignature!=null) {
                            result.setSignature(wxJsapiSignature.getSignature());
                            result.setNonceStr(wxJsapiSignature.getNonceStr());
                            result.setTimestamp(wxJsapiSignature.getTimestamp());
                        }
                    }
                }else if(WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_DK.getCode() == openAuthDTO.getConfigSubType()){
                    WxCpTpService wxCpTpService = getBean().getWxCpTpService(tId, appId);
                    if(StringUtils.isEmpty(query.getCorpId())) {
                        WxJsapiSignature wxJsapiSignature = wxCpTpService.createSuiteJsApiTicketSignature(url, query.getCorpId());
                        if(wxJsapiSignature!=null) {
                            result.setSignature(wxJsapiSignature.getSignature());
                            result.setNonceStr(wxJsapiSignature.getNonceStr());
                            result.setTimestamp(wxJsapiSignature.getTimestamp());
                        }
                    }else{
                        WxJsapiSignature wxJsapiSignature = wxCpTpService.createAuthCorpJsApiTicketSignature(url, query.getCorpId());
                        if(wxJsapiSignature!=null) {
                            result.setSignature(wxJsapiSignature.getSignature());
                            result.setNonceStr(wxJsapiSignature.getNonceStr());
                            result.setTimestamp(wxJsapiSignature.getTimestamp());
                        }
                    }
                }
            }
        }catch(WxErrorException ex){
            throw new BusinessException("创建签名出错！", ex);
        }
        return result;
    }
    /**
     * 获取开放平台服务并将注册为bean
     * @param
     * @return
     */
    @Override
    public WxOpenService getWxOpenService(Long configId) {
        WxOpenService wxOpenService = SpringBeanUtil.getBean(WxOpenService.class);
        if(wxOpenService == null){
            OpenConfigVO openConfigVO = openConfigServiceImpl.view(configId);

            wxOpenService = new me.chanjar.weixin.open.api.impl.WxOpenServiceImpl();
            WxOpenConfigStorage wxOpenConfigStorage = new WxOpenInRedisConfigStorage(redisProperties.getJedisPool());
            wxOpenConfigStorage.setComponentAppId(openConfigVO.getComponentAppId());
            wxOpenConfigStorage.setComponentAesKey(openConfigVO.getComponentMsgAesKey());
            wxOpenConfigStorage.setComponentAppSecret(openConfigVO.getComponentSecret());
            wxOpenConfigStorage.setComponentToken(openConfigVO.getComponentMsgToken());
            OpenConfigOfExtend openConfigOfExtend = openConfigVO.getOpenConfigOfExtend();
            if(openConfigOfExtend!=null) {
                OpenConfigOfWxOpen openConfigOfWxOpen = openConfigOfExtend.getOpenConfigOfWxOpen();
                if(openConfigOfWxOpen!=null) {
                    wxOpenConfigStorage.setComponentVerifyTicket(openConfigOfWxOpen.getComponentVerifyTicket());
                }
            }

            wxOpenService.setWxOpenConfigStorage(wxOpenConfigStorage);
        }

        return wxOpenService;
    }
    /**
     * 获取开放平台服务并将注册为bean
     * @param
     * @return
     */
    @Override
    public WxOpenMaService getWxOpenMaService(Long tenantId, String appId) {
        OpenAuthQuery query = new OpenAuthQuery();
        query.setTenantId(tenantId);
        query.setAppId(appId);
        OpenAuthDTO openAuthDTO = openAuthServiceImpl.queryOpenAuthOne(query);
        if(openAuthDTO ==null ) throw new BusinessException("读取租户的开放平台参数openAuthDTO出错！");
        OpenAuthOfExtend openAuthOfExtend = openAuthDTO.getOpenAuthOfExtend();
        if(openAuthOfExtend ==null ) throw new BusinessException("读取租户的开放平台参数openAuthOfExtend出错！");
        OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
        if(openAuthOfWxOpen ==null ) throw new BusinessException("读取租户的开放平台参数openAuthOfWxOpen出错！");
        WxOpenService wxOpenService = getBean().getWxOpenService(openAuthDTO.getOpenId());
        WxOpenConfigStorage wxOpenConfigStorage = wxOpenService.getWxOpenConfigStorage();
        wxOpenConfigStorage.setComponentVerifyTicket(openAuthOfWxOpen.getComponentVerifyTicket());
        wxOpenConfigStorage.setComponentToken(openAuthOfWxOpen.getComponentAccessToken());
        wxOpenConfigStorage.setAuthorizerRefreshToken(appId, openAuthOfWxOpen.getAuthorizerRefreshToken());
        wxOpenConfigStorage.updateAuthorizerAccessToken(appId, openAuthOfWxOpen.getAuthorizerAccessToken(), openAuthOfWxOpen.getExpireTime());
        WxOpenComponentService wxOpenComponentService = wxOpenService.getWxOpenComponentService();
        if(wxOpenComponentService ==null ) throw new BusinessException("初始化wxOpenComponentService出错！");
        WxOpenMaService wxOpenMaService = wxOpenComponentService.getWxMaServiceByAppid(appId);
        if(wxOpenMaService ==null ) throw new BusinessException("初始化wxOpenMaService出错！");

        return wxOpenMaService;
    }
    /**
     * 获取开放平台服务并将注册为bean
     * @param
     * @return
     */
    @Override
    public WxOpenMpService getWxOpenMpService(Long tenantId, String appId) {
        OpenAuthQuery query = new OpenAuthQuery();
        query.setTenantId(tenantId);
        query.setAppId(appId);
        OpenAuthDTO openAuthDTO = openAuthServiceImpl.queryOpenAuthOne(query);
        if(openAuthDTO ==null ) throw new BusinessException("读取租户的开放平台参数openAuthDTO出错！");
        OpenAuthOfExtend openAuthOfExtend = openAuthDTO.getOpenAuthOfExtend();
        if(openAuthOfExtend ==null ) throw new BusinessException("读取租户的开放平台参数openAuthOfExtend出错！");
        OpenAuthOfWxOpen openAuthOfWxOpen = openAuthOfExtend.getOpenAuthOfWxOpen();
        if(openAuthOfWxOpen ==null ) throw new BusinessException("读取租户的开放平台参数openAuthOfWxOpen出错！");
        WxOpenService wxOpenService = getBean().getWxOpenService(openAuthDTO.getOpenId());
        WxOpenConfigStorage wxOpenConfigStorage = wxOpenService.getWxOpenConfigStorage();
        wxOpenConfigStorage.setComponentVerifyTicket(openAuthOfWxOpen.getComponentVerifyTicket());
        wxOpenConfigStorage.setComponentToken(openAuthOfWxOpen.getComponentAccessToken());
        wxOpenConfigStorage.setAuthorizerRefreshToken(appId, openAuthOfWxOpen.getAuthorizerRefreshToken());
        wxOpenConfigStorage.updateAuthorizerAccessToken(appId, openAuthOfWxOpen.getAuthorizerAccessToken(), openAuthOfWxOpen.getExpireTime());
        WxOpenComponentService wxOpenComponentService = wxOpenService.getWxOpenComponentService();
        if(wxOpenComponentService ==null ) throw new BusinessException("初始化wxOpenComponentService出错！");
        WxOpenMpService wxOpenMpService = wxOpenComponentService.getWxMpServiceByAppid(appId);
        if(wxOpenMpService ==null ) throw new BusinessException("初始化wxOpenMaService出错！");

        return wxOpenMpService;
    }
    /**
     * 获取开放平台服务并将注册为bean
     * @param
     * @return
     */
    @Override
    public WxCpService getWxCpService(Long tenantId, String appId) {
        OpenAuthQuery authQuery = new OpenAuthQuery();
        authQuery.setTenantId(tenantId);
        authQuery.setAppId(appId);
        OpenAuthDTO openAuthDTO = openAuthServiceImpl.queryOpenAuthOne(authQuery);
        if(openAuthDTO ==null ) throw new BusinessException("读取租户的开放平台参数openAuthDTO出错！");
        OpenConfigQuery configQuery = new OpenConfigQuery();
        configQuery.setId(openAuthDTO.getOpenId());
        OpenConfigDTO openConfigDTO = openConfigServiceImpl.queryOpenConfigOne(configQuery);
        if(openConfigDTO ==null ) throw new BusinessException("读取租户的开放平台参数openConfigVO出错！");
        WxCpService wxCpService = SpringBeanUtil.getBean(WxCpService.class);
        if(wxCpService == null) {
            wxCpService = new WxCpServiceImpl();
        }
        WxCpConfigStorage wxCpConfigStorage = getWxCpRedissonConfigStorage(tenantId, openConfigDTO, openAuthDTO);
        wxCpService.setWxCpConfigStorage(wxCpConfigStorage);

        return wxCpService;
    }
    /**
     * 获取开放平台服务并将注册为bean
     * @param
     * @return
     */
    @Override
    public WxCpTpService getWxCpTpService(Long tenantId, String appId) {
        OpenAuthQuery authQuery = new OpenAuthQuery();
        authQuery.setTenantId(tenantId);
        authQuery.setAppId(appId);
        OpenAuthDTO openAuthDTO = openAuthServiceImpl.queryOpenAuthOne(authQuery);
        if(openAuthDTO ==null ) throw new BusinessException("读取租户的开放平台参数openAuthDTO出错！");
        OpenConfigQuery configQuery = new OpenConfigQuery();
        configQuery.setId(openAuthDTO.getOpenId());
        OpenConfigDTO openConfigDTO = openConfigServiceImpl.queryOpenConfigOne(configQuery);
        if(openConfigDTO ==null ) throw new BusinessException("读取租户的开放平台参数openConfigVO出错！");
        WxCpTpService wxCpTpService = SpringBeanUtil.getBean(WxCpTpService.class);
        if(wxCpTpService == null) {
            wxCpTpService = new WxCpTpServiceImpl();
        }
        WxCpTpConfigStorage wxCpTpConfigStorage = getWxCpTpRedissonConfigStorage(tenantId, openConfigDTO, openAuthDTO);
        wxCpTpService.setWxCpTpConfigStorage(wxCpTpConfigStorage);

        return wxCpTpService;
    }
    /**
     * 获取开放平台服务并将注册为bean
     * @param
     * @return
     */
    @Override
    public WxCpTpService getWxCpDkService(Long tenantId, String appId) {
        OpenAuthQuery authQuery = new OpenAuthQuery();
        authQuery.setTenantId(tenantId);
        authQuery.setAppId(appId);
        OpenAuthDTO openAuthDTO = openAuthServiceImpl.queryOpenAuthOne(authQuery);
        if(openAuthDTO ==null ) throw new BusinessException("读取租户的开放平台参数openAuthDTO出错！");
        OpenConfigQuery configQuery = new OpenConfigQuery();
        configQuery.setId(openAuthDTO.getOpenId());
        OpenConfigDTO openConfigDTO = openConfigServiceImpl.queryOpenConfigOne(configQuery);
        if(openConfigDTO ==null ) throw new BusinessException("读取租户的开放平台参数openConfigVO出错！");
        WxCpTpService wxCpTpService = SpringBeanUtil.getBean(WxCpTpService.class);
        if(wxCpTpService == null) {
            wxCpTpService = new WxCpTpServiceImpl();
        }
        WxCpTpConfigStorage wxCpDkConfigStorage = getWxCpDkRedissonConfigStorage(tenantId, openConfigDTO, openAuthDTO);
        wxCpTpService.setWxCpTpConfigStorage(wxCpDkConfigStorage);

        return wxCpTpService;
    }

    private WxCpConfigStorage getWxCpRedissonConfigStorage(Long tenantId, OpenConfigDTO wxOpenConfigDTO, OpenAuthDTO wxOpenAuthDTO) {
        OpenAuthOfExtend openAuthOfExtend = wxOpenAuthDTO.getOpenAuthOfExtend();
        if(openAuthOfExtend == null) throw new BusinessException("企业微信自建应用没有配置，请先配置!");
        OpenAuthOfWxCpSb  openAuthOfWxCpSb = openAuthOfExtend.getOpenAuthOfWxCpSb();
        WxCpJedisConfigImpl wxCpJedisConfigImpl = new WxCpJedisConfigImpl(redisProperties.getJedisPool(), "wx:cp:sb:".concat(tenantId.toString()));

        wxCpJedisConfigImpl.setCorpId(wxOpenAuthDTO.getOpenAppId());
        wxCpJedisConfigImpl.setCorpSecret(wxOpenAuthDTO.getAppSecret());
        wxCpJedisConfigImpl.setToken(wxOpenAuthDTO.getAppToken());
        wxCpJedisConfigImpl.setAesKey(wxOpenAuthDTO.getAppAesKey());
        OpenConfigOfExtend openConfigOfExtend = wxOpenConfigDTO!=null?wxOpenConfigDTO.getOpenConfigOfExtend():null;
        OpenConfigOfWxCp openConfigOfWxCp = openConfigOfExtend!=null?openConfigOfExtend.getOpenConfigOfWxCp():null;
        OpenConfigOfWxCpTalkData openConfigOfWxCpTalkData = openConfigOfWxCp!=null?openConfigOfWxCp.getWxCpTalkData():null;
        if(openConfigOfWxCpTalkData!=null) {
            wxCpJedisConfigImpl.setMsgAuditSecret(openConfigOfWxCpTalkData.getTalkDataSecret());
            wxCpJedisConfigImpl.setMsgAuditPriKey(openConfigOfWxCpTalkData.getTalkDataPriKey());
            wxCpJedisConfigImpl.setMsgAuditLibPath(openConfigOfWxCpTalkData.getTalkDataLibPath());
        }
        if(StringUtils.isNotEmpty(wxOpenAuthDTO.getAppId())) {
            Integer agentId  = Integer.parseInt(wxOpenAuthDTO.getAppId());
            wxCpJedisConfigImpl.setAgentId(agentId);
        }
        if(StringUtils.isNotEmpty(openAuthOfWxCpSb.getAccessToken())){
            WxAccessToken wxAccessToken = new WxAccessToken();
            wxAccessToken.setAccessToken(openAuthOfWxCpSb.getAccessToken());
            wxAccessToken.setExpiresIn(openAuthOfWxCpSb.getAccessTokenExpiresTime());
            wxCpJedisConfigImpl.updateAccessToken(wxAccessToken);
        }
        return wxCpJedisConfigImpl;
    }

    private WxCpTpConfigStorage getWxCpTpRedissonConfigStorage(Long tenantId, OpenConfigDTO openConfigDTO, OpenAuthDTO wxOpenAuthDTO) {
        Integer configType = OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode();
        Integer configSubType = WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_TP.getCode();
        String openConfigKey = "wx:cp:tp:t".concat(configType.toString()).concat(":s").concat(configSubType.toString());
        OpenAuthOfExtend openAuthOfExtend = wxOpenAuthDTO.getOpenAuthOfExtend();
        if(openAuthOfExtend == null) throw new BusinessException("企业微信三方应用没有配置，请先配置!");
        OpenAuthOfWxCpTp  openAuthOfWxCpTp = openAuthOfExtend.getOpenAuthOfWxCpTp();

        JedisWxRedisOps wxRedisOps = new JedisWxRedisOps(redisProperties.getJedisPool());
        String suiteId = openConfigDTO.getComponentAppId();
        WxCpTpRedissonConfigImpl.WxCpTpRedissonConfigImplBuilder configStorageBuider = WxCpTpRedissonConfigImpl.builder().wxRedisOps(wxRedisOps);

        if(StringUtils.isNotEmpty(openConfigKey))configStorageBuider.keyPrefix(openConfigKey);
        if(StringUtils.isNotEmpty(openConfigDTO.getOpenAppId()))configStorageBuider.corpId(openConfigDTO.getOpenAppId());
        if(StringUtils.isNotEmpty(openConfigDTO.getOpenAppSecret()))configStorageBuider.corpSecret(openConfigDTO.getOpenAppSecret());
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentAppId()))configStorageBuider.suiteId(suiteId);
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentSecret()))configStorageBuider.suiteSecret(openConfigDTO.getComponentSecret());
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentMsgToken()))configStorageBuider.token(openConfigDTO.getComponentMsgToken());
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentMsgAesKey()))configStorageBuider.aesKey(openConfigDTO.getComponentMsgAesKey());
        if(StringUtils.isNotEmpty(openConfigDTO.getHttpProxyHost()))configStorageBuider.httpProxyHost(openConfigDTO.getHttpProxyHost());
        if(null!=openConfigDTO.getHttpProxyPort())configStorageBuider.httpProxyPort(openConfigDTO.getHttpProxyPort());
        if(StringUtils.isNotEmpty(openConfigDTO.getHttpProxyUsername()))configStorageBuider.httpProxyUsername(openConfigDTO.getHttpProxyUsername());
        if(StringUtils.isNotEmpty(openConfigDTO.getHttpProxyPassword()))configStorageBuider.httpProxyPassword(openConfigDTO.getHttpProxyPassword());

        WxCpTpConfigStorage configStorage = configStorageBuider.build();

        Object suiteTicket = redisUtils.get(RedisKeyConstant.WX_CP_TP_SUITE_TICKET.concat(suiteId));
        Object suiteAccessTokenObj = redisUtils.get(RedisKeyConstant.WX_CP_TP_SUITE_ACCESS_TOKEN.concat(suiteId));

        if(ObjectUtils.isNotEmpty(suiteTicket)){
            configStorage.updateSuiteTicket(suiteTicket.toString(), 1200);
        }
        if(ObjectUtils.isNotEmpty(suiteAccessTokenObj)) {
            WxAccessToken suiteAccessTokenEntity = (WxAccessToken)suiteAccessTokenObj;
            if(StringUtils.isNotEmpty(suiteAccessTokenEntity.getAccessToken())) {
                configStorage.updateSuiteAccessToken(suiteAccessTokenEntity);
            }else{
                OpenConfigOfExtend openConfigOfExtend = openConfigDTO!=null?openConfigDTO.getOpenConfigOfExtend():null;
                OpenConfigOfWxCp openConfigOfWxCp = openConfigOfExtend != null ?openConfigOfExtend.getOpenConfigOfWxCp():null;
                suiteAccessTokenEntity.setAccessToken(openConfigOfWxCp.getComponentAccessToken());
                suiteAccessTokenEntity.setExpiresIn(7200);
                configStorage.updateSuiteAccessToken(suiteAccessTokenEntity);
            }
        }
        return configStorage;
    }

    private WxCpTpConfigStorage getWxCpDkRedissonConfigStorage(Long tenantId, OpenConfigDTO openConfigDTO, OpenAuthDTO wxOpenAuthDTO) {
        Integer configType = OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode();
        Integer configSubType = WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_DK.getCode();
        String openConfigKey = "wx:cp:dk:t".concat(configType.toString()).concat(":s").concat(configSubType.toString());
        OpenAuthOfExtend openAuthOfExtend = wxOpenAuthDTO.getOpenAuthOfExtend();
        if(openAuthOfExtend == null) throw new BusinessException("企业微信三方应用没有配置，请先配置!");
        OpenAuthOfWxCpTp  openAuthOfWxCpTp = openAuthOfExtend.getOpenAuthOfWxCpTp();

        OpenConfigOfExtend openConfigOfExtend = openConfigDTO!=null?openConfigDTO.getOpenConfigOfExtend():null;
        OpenConfigOfWxCp openConfigOfWxCp = openConfigOfExtend != null ?openConfigOfExtend.getOpenConfigOfWxCp():null;
        JedisWxRedisOps wxRedisOps = new JedisWxRedisOps(redisProperties.getJedisPool());
        String suiteId = openConfigDTO.getComponentAppId();
        WxCpTpRedissonConfigImpl.WxCpTpRedissonConfigImplBuilder configStorageBuider = WxCpTpRedissonConfigImpl.builder().wxRedisOps(wxRedisOps);
        if(StringUtils.isNotEmpty(openConfigKey))configStorageBuider.keyPrefix(openConfigKey);
        if(StringUtils.isNotEmpty(openConfigDTO.getOpenAppId()))configStorageBuider.corpId(openConfigDTO.getOpenAppId());
        if(StringUtils.isNotEmpty(openConfigDTO.getOpenAppSecret()))configStorageBuider.corpSecret(openConfigDTO.getOpenAppSecret());
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentAppId()))configStorageBuider.suiteId(suiteId);
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentSecret()))configStorageBuider.suiteSecret(openConfigDTO.getComponentSecret());
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentMsgToken()))configStorageBuider.token(openConfigDTO.getComponentMsgToken());
        if(StringUtils.isNotEmpty(openConfigDTO.getComponentMsgAesKey()))configStorageBuider.aesKey(openConfigDTO.getComponentMsgAesKey());
        if(StringUtils.isNotEmpty(openConfigDTO.getHttpProxyHost()))configStorageBuider.httpProxyHost(openConfigDTO.getHttpProxyHost());
        if(null!=openConfigDTO.getHttpProxyPort())configStorageBuider.httpProxyPort(openConfigDTO.getHttpProxyPort());
        if(StringUtils.isNotEmpty(openConfigDTO.getHttpProxyUsername()))configStorageBuider.httpProxyUsername(openConfigDTO.getHttpProxyUsername());
        if(StringUtils.isNotEmpty(openConfigDTO.getHttpProxyPassword()))configStorageBuider.httpProxyPassword(openConfigDTO.getHttpProxyPassword());

        OpenConfigOfWxCpProvider wxCpProvider = openConfigOfWxCp!=null?openConfigOfWxCp.getWxCpProvider():null;
        String providerSecret = wxCpProvider!=null?wxCpProvider.getProviderSecret():null;
        if(StringUtils.isNotEmpty(providerSecret))configStorageBuider.providerSecret(providerSecret);

        WxCpTpConfigStorage configStorage = configStorageBuider.build();

        String suiteTicket = redisUtils.getString(RedisKeyConstant.WX_CP_DK_SUITE_TICKET.concat(suiteId));
        Object suiteAccessTokenObj = redisUtils.get(RedisKeyConstant.WX_CP_DK_SUITE_ACCESS_TOKEN.concat(suiteId));

        if(StringUtils.isNotEmpty(suiteTicket)){
            configStorage.updateSuiteTicket(suiteTicket, 1200);
        }
        if(ObjectUtils.isNotEmpty(suiteAccessTokenObj)) {
            WxAccessToken suiteAccessTokenEntity = (WxAccessToken)suiteAccessTokenObj;
            if(StringUtils.isNotEmpty(suiteAccessTokenEntity.getAccessToken())) {
                configStorage.updateSuiteAccessToken(suiteAccessTokenEntity);
            }else{
                suiteAccessTokenEntity.setAccessToken(openConfigOfWxCp.getComponentAccessToken());
                suiteAccessTokenEntity.setExpiresIn(7200);
                configStorage.updateSuiteAccessToken(suiteAccessTokenEntity);
            }
        }
        return configStorage;
    }

    private WxOpenServiceImpl getBean() {
        return SpringBeanUtil.getBean(this.getClass());
    }
}